﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace DeepEarth.Client.MapControl.Utilities
{
    public class AnimateBlur
    {
        public static void Blur(Map map)
        {
            var curLevel = Math.Log(map.ZoomLevel, 2);
            var targetBlur = curLevel - (int)curLevel;
            targetBlur = (1 / (2 - targetBlur)) * 2;

            var sb = new Storyboard();
            var kt = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1.5));

            // set up the viewport animation
            var daf = new DoubleAnimationUsingKeyFrames();
            var sdkf = new SplineDoubleKeyFrame { Value = targetBlur, KeyTime = kt };
            var ksf = new KeySpline { ControlPoint1 = new Point(0.15, 0.475), ControlPoint2 = new Point(0, 1) };
            sdkf.KeySpline = ksf;
            daf.KeyFrames.Add(sdkf);
            Storyboard.SetTarget(daf, map.BaseLayer.Msi);
            Storyboard.SetTargetProperty(daf, new PropertyPath("BlurFactor"));
            sb.Children.Add(daf);
            sb.Begin();
        }
    }
}